package com.kaili.basic.controlle;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.pojo.dto.SystemParamAddDto;
import com.kaili.basic.pojo.dto.SystemParamEditDto;
import com.kaili.basic.pojo.dto.SystemParamQueryDto;
import com.kaili.basic.pojo.entity.SystemParam;
import com.kaili.basic.service.SystemParamService;
import com.kaili.basic.version.ApiVersion;
import com.kaili.common.response.CommonStatus;
import com.kaili.common.response.RestStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * Created by renzhikun on 18-9-28.
 */
@ApiVersion(1)
@RequestMapping("/{api_version}")
@RestController
@Api(tags = "系统参数管理", description = "系统参数管理相关API")
public class SystemParamController {

    @Autowired
    private SystemParamService systemParamService;

    @PostMapping("/systemParams")
    @ApiOperation(value = "新增", notes = "新增系统参数")
    public RestStatus addSystemParam(@Validated @RequestBody SystemParamAddDto addDto) {
        SystemParam systemParam=new SystemParam();
        BeanUtils.copyProperties(addDto,systemParam);
        if (systemParam == null) {
            return CommonStatus.BAD_REQUEST;
        }
        if (systemParamService.getSystemParam(systemParam) != null) {
            return CommonStatus.ADD_DEFEAT;
        }
        systemParamService.addSystemParam(systemParam);
        return CommonStatus.ADD_OK;
    }

    @DeleteMapping("/systemParams/{paramId}")
    @ApiOperation(value = "删除", notes = "删除系统参数")
    public RestStatus deleteSystemParam(@ApiParam("主键ID") @PathVariable ("paramId")Integer paramId) {
        if (paramId == null) {
            return CommonStatus.BAD_REQUEST;
        }
        systemParamService.deleteSystemParam(paramId);
        return CommonStatus.DELETE_OK;
    }

    @PutMapping("/systemParams")
    @ApiOperation(value = "更新", notes = "更新系统参数")
    public RestStatus updateSystemParam(@Validated @RequestBody SystemParamEditDto editDto) {
        SystemParam systemParam=new SystemParam();
        BeanUtils.copyProperties(editDto,systemParam);
        SystemParam temp = systemParamService.getSystemParam(systemParam);
        if (temp != null && !Objects.equals(temp.getParamId(), systemParam.getParamId())) {
            return CommonStatus.ADD_DEFEAT;
        }
        if (systemParam != null) {
            systemParamService.updateSystemParam(systemParam);
            return CommonStatus.UPDATE_OK;
        }
        return CommonStatus.BAD_REQUEST;
    }

    @GetMapping("/systemParams")
    @ApiOperation(value = "分页查询系统参数", notes = "分页查询事件")
    public Page<SystemParam> querySystemParam(@ModelAttribute SystemParamQueryDto queryDto) {
        SystemParam systemParam=new SystemParam();
        BeanUtils.copyProperties(queryDto,systemParam);
        return systemParamService.querySystemParam(new Page<SystemParam>(systemParam.getPage(), systemParam.getPageSize()), systemParam);
    }
}
